1
基本検索を超えて:意味的類似性の限界に対処する
AI010Lesson 8
00:00

類似性を超えて

80%の問題基本的な意味的検索は簡単なクエリでは機能しますが、特殊なケースでは失敗します。類似性だけで検索すると、ベクトルストアはしばしば数値的に最も類似したチャンクを返します。しかし、これらのチャンクがほぼ同一の場合、LLMは重複情報を受信し、限られたコンテキストウィンドウを無駄に使い、広い視点を見逃す結果になります。

高度な取得の柱

  1. 最大マージナル関連性(MMR):最も類似したアイテムを選ぶだけではなく、関連性と多様性のバランスを取ることで、重複を回避します。
    $$MMR = \text{argmax}_{d \in R \setminus S} [\lambda \cdot \text{sim}(d, q) - (1 - \lambda) \cdot \max_{s \in S} \text{sim}(d, s)]$$
  2. セルフクエリリング:LLMを使用して自然言語を構造化されたメタデータフィルター(例:「第3講義」や「ソース:PDF」によるフィルタリング)に変換します。
  3. 文脈圧縮:取得したドキュメントを縮小し、クエリに関連する「高栄養価」のスニペットのみを抽出することで、トークンを節約します。
冗長性の罠
同じ段落の3つのバージョンをLLMに提供しても、より賢くなるわけではありません。むしろプロンプトのコストが高くなるだけです。多様性こそが「高栄養価」なコンテキストの鍵です。
retrieval_advanced.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
Knowledge Check
You want your system to answer "What did the instructor say about probability in the third lecture?" specifically. Which tool allows the LLM to automatically apply a filter for { "source": "lecture3.pdf" }?
ConversationBufferMemory
Self-Querying Retriever
Contextual Compression
MapReduce Chain
Challenge: The Token Limit Dilemma
Apply advanced retrieval strategies to solve a real-world constraint.
You are building a RAG system for a legal firm. The documents retrieved are 50 pages long, but only 2 sentences per page are actually relevant to the user's specific query. The standard "Stuff" chain is throwing an OutOfTokens error because the context window is overflowing with irrelevant text.
Step 1
Identify the core problem and select the appropriate advanced retrieval tool to solve it without losing specific nuances.
Problem: The context window limit is being exceeded by "low-nutrient" text surrounding the relevant facts.

Tool Selection:ContextualCompressionRetriever
Step 2
What specific component must you use in conjunction with this retriever to "squeeze" the documents?
Solution: Use an LLMChainExtractor as the base for your compressor. This will process the retrieved documents and extract only the snippets relevant to the query, passing a much smaller, highly concentrated context to the final prompt.